package com.foreknow.demo13;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.setName("fender");
        Person p2 = new Person();
        p2.setName("gibson");
        Person p3 = new Person();
        p3.setName("BOSS");
        //创建一个Map容器
        Map<Integer,Person> maps = new HashMap<>();
        //向Map容器中添加元素
        maps.put(1,p1);
        maps.put(2,p2);
        maps.put(3,p3);
        //从Map容器中获取某个元素
        Person pp = maps.get(2);
        System.out.println(pp.getName());
        //遍历Map集合
        Set<Map.Entry<Integer,Person>> s = maps.entrySet(); //因为Map中没有迭代器，所以通过调用entrySet()获取到Set集合
        Iterator<Map.Entry<Integer,Person>> it = s.iterator(); //获取迭代器
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            Integer key = (Integer) entry.getKey();
            Person value = (Person) entry.getValue();
            System.out.println(key+"-----"+value.getName());
        }

    }
}
